package com.litidea.dao.impl;

import static com.litidea.commons.dict.StaticDics.CHANGE_EMERGENCY_STATUS;
import static com.litidea.commons.dict.StaticDics.CHANGE_REASON;
import static com.litidea.commons.dict.StaticDics.CHANGE_TICKET_STATUS;
import static com.litidea.commons.dict.StaticDics.EVALUATION_RESULT;
import static com.litidea.commons.dict.StaticDics.EXTERNAL_CHANGE_TICKET_STATUS;
import static com.litidea.commons.dict.StaticDics.IMPACT_FOR_PRODUCE_HANDLE;
import static com.litidea.commons.dict.StaticDics.IMPACT_FOR_RELIABILITY;
import static com.litidea.commons.dict.StaticDics.OK_MAP;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.litidea.commons.utils.JSONUtils;
import com.litidea.dao.ChangeHistoryDAO;
import com.litidea.dao.abs.ChangeHistoryDAOAbs;
import com.litidea.dataobjects.ChangeTypeTestItemsDO;
import com.litidea.dataobjects.ExternalChangeDO;
import com.litidea.dataobjects.InternalChangeDO;

@SuppressWarnings("unchecked")
public class ChangeHistoryDAOImpl extends ChangeHistoryDAOAbs implements
		ChangeHistoryDAO {

	/**
	 * 获取变更历史数量数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getChangeHistoryCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"changehistory.getChangeHistoryCount", params);
	}

	@Override
	public JSONObject getChangeHistoryPageList(Map<String, String> params) {
		// TODO Auto-generated method stub

		params.put("status", "12");
		List<InternalChangeDO> list = this.getSqlMapClientTemplate()
				.queryForList("changehistory.getInternalChanges", params);

		for (InternalChangeDO internalChangeDO : list) {
			internalChangeDO.setEmergencyString(CHANGE_EMERGENCY_STATUS
					.get(String.valueOf(internalChangeDO.getEmergency())));
			internalChangeDO.setReasonString(CHANGE_REASON.get(String
					.valueOf(internalChangeDO.getReasonCode())));
			internalChangeDO.setStatusString(CHANGE_TICKET_STATUS.get(String
					.valueOf(internalChangeDO.getStatus())));
			if (internalChangeDO.getEffective() == 1)
				internalChangeDO.setEffectiveString("长期");
			else {
				internalChangeDO.setEffectiveString("临时<br/>起始日期: "
						+ internalChangeDO.getEffectiveStartDate() + " -- "
						+ internalChangeDO.getEffectiveEndDate());
			}
			internalChangeDO
					.setEvaImpactForReliabilityString(IMPACT_FOR_RELIABILITY
							.get(String.valueOf(internalChangeDO
									.getEvaImpactForReliability())));
			internalChangeDO
					.setEvaImpactForProductHandleString(IMPACT_FOR_PRODUCE_HANDLE
							.get(String.valueOf(internalChangeDO
									.getEvaImpactForProductHandle())));
			internalChangeDO.setMustVerifyString(OK_MAP.get(internalChangeDO
					.getMustVerify()));
			internalChangeDO.setMustAlertCustomerString(OK_MAP
					.get(internalChangeDO.getMustAlertCustomer()));
			internalChangeDO.setMustSpplierChangeString(OK_MAP
					.get(internalChangeDO.getMustSpplierChange()));
			internalChangeDO.setResultString(EVALUATION_RESULT
					.get(internalChangeDO.getResult()));
			internalChangeDO.setSampleIsQualifiedString(OK_MAP
					.get(internalChangeDO.getSampleIsQualified()));
			internalChangeDO.setSmallIsQualifiedString(OK_MAP
					.get(internalChangeDO.getSmallIsQualified()));
			internalChangeDO.setMiddleIsQualifiedString(OK_MAP
					.get(internalChangeDO.getMiddleIsQualified()));
			internalChangeDO.setIsFinishedAlertCustomerString(OK_MAP
					.get(internalChangeDO.getIsFinishedAlertCustomer()));
			internalChangeDO.setIsFinishedSupChangeString(OK_MAP
					.get(internalChangeDO.getIsFinishedSupChange()));
			internalChangeDO.setIsFinishedSecurityString(OK_MAP
					.get(internalChangeDO.getIsFinishedSecurity()));
			internalChangeDO.setChangeResultIsQualifyString(OK_MAP
					.get(internalChangeDO.getChangeResultIsQualify()));
			internalChangeDO.setIsApproveChangeString(OK_MAP
					.get(internalChangeDO.getIsApproveChange()));

			internalChangeDO.setResearchDeptIsBomChangedString(OK_MAP
					.get(internalChangeDO.getResearchDeptIsBomChanged()));
			internalChangeDO.setResearchDeptIsDesignDocChangedString(OK_MAP
					.get(internalChangeDO.getResearchDeptIsDesignDocChanged()));
			internalChangeDO.setResearchDeptIsDrawingChnagedString(OK_MAP
					.get(internalChangeDO.getResearchDeptIsDrawingChnaged()));

			internalChangeDO.setEngineeringDeptIsBomChangedString(OK_MAP
					.get(internalChangeDO.getEngineeringDeptIsBomChanged()));
			internalChangeDO.setEngineeringDeptIsMoldChangedString(OK_MAP
					.get(internalChangeDO.getEngineeringDeptIsMoldChanged()));
			internalChangeDO.setEngineeringDeptIsProcChangedString(OK_MAP
					.get(internalChangeDO.getEngineeringDeptIsProcChanged()));
			internalChangeDO.setEngineeringDeptIsQcChangedString(OK_MAP
					.get(internalChangeDO.getEngineeringDeptIsQcChanged()));
			internalChangeDO.setEngineeringDeptIsSopChangedString(OK_MAP
					.get(internalChangeDO.getEngineeringDeptIsSopChanged()));

			internalChangeDO.setQualityDeptIsInspectDocChangedString(OK_MAP
					.get(internalChangeDO.getQualityDeptIsInspectDocChanged()));
			internalChangeDO.setQualityDeptIsMarkInFirst3String(OK_MAP
					.get(internalChangeDO.getQualityDeptIsMarkInFirst3()));
			internalChangeDO.setQualityDpetIsTrainedString(OK_MAP
					.get(internalChangeDO.getQualityDpetIsTrained()));
			internalChangeDO.setQualityDpetIsUpgradeInspectionString(OK_MAP
					.get(internalChangeDO.getQualityDpetIsUpgradeInspection()));

			internalChangeDO.setProductDeptIsMaterialCheckedString(OK_MAP
					.get(internalChangeDO.getProductDeptIsMaterialChecked()));
			internalChangeDO.setProductDeptIsRegistedString(OK_MAP
					.get(internalChangeDO.getProductDeptIsRegisted()));
			internalChangeDO.setProductDeptIsTrainedString(OK_MAP
					.get(internalChangeDO.getProductDeptIsTrained()));

			internalChangeDO.setSummaryIsImpactShipMentString(OK_MAP
					.get(internalChangeDO.getSummaryIsImpactShipMent()));
			internalChangeDO
					.setSummaryIsNumberBeforeChangeProdString(OK_MAP
							.get(internalChangeDO
									.getSummaryIsNumberBeforeChangeProd()));
			internalChangeDO.setSummaryIsNumberBeforeChangeSupString(OK_MAP
					.get(internalChangeDO.getSummaryIsNumberBeforeChangeSup()));

			// 组装实验项目字符串
			List<ChangeTypeTestItemsDO> testItemsList = this
					.getTestItemssByChangeId(internalChangeDO.getId());
			String temp = "<table class=\"table\" style=\"table-layout:fixed;word-break: break-all;word-wrap:break-word;\" width=\"100%\"><tbody><tr><td  width=\"50%\">实验项目名称</td><td>是否必须</td></tr>";
			for (ChangeTypeTestItemsDO changeTypeTestItemsDO : testItemsList) {
				temp += "<tr><td>" + changeTypeTestItemsDO.getTestItemsName()
						+ "</td><td>" + changeTypeTestItemsDO.getIsMustString()
						+ "</td></tr>";
			}
			temp += "</tbody></table>";

			internalChangeDO.setTestItemsString(temp);
		}
		return JSONUtils.getPageJSON(list, this.getChangeHistoryCount(params));
	}

	private List<ChangeTypeTestItemsDO> getTestItemssByChangeId(Integer changeId) {
		// TODO Auto-generated method stub
		List<ChangeTypeTestItemsDO> list = this.getSqlMapClientTemplate()
				.queryForList("change.getTestItemsByChangeId", changeId);

		for (ChangeTypeTestItemsDO changeTypeTestItemsDO : list) {
			changeTypeTestItemsDO.setIsMustString(OK_MAP
					.get(changeTypeTestItemsDO.getIsMust()));
		}

		return list;
	}

	@Override
	public String changeAttachment(Map<String, String> params) {
		// TODO Auto-generated method stub
		String flag = params.get("type");
		if (flag.equals("1")) {
			// 样本报告
			this.getSqlMapClientTemplate().update(
					"changehistory.updateSampleReport", params);
		} else if (flag.equals("2")) {
			// 小批量报告
			this.getSqlMapClientTemplate().update(
					"changehistory.updateSmallReport", params);
		} else if (flag.equals("3")) {
			// 中批量报告
			this.getSqlMapClientTemplate().update(
					"changehistory.updateMiddleReport", params);
		} else if (flag.equals("4")) {
			// 通报客户和供应商附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateAlertAttachment", params);
		} else if (flag.equals("5")) {
			// 应研所附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateResearchDeptAttachment", params);
		} else if (flag.equals("6")) {
			// 工程部附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateEngineeringDeptAttachment", params);
		} else if (flag.equals("7")) {
			// 质量部附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateQualityDeptAttachment", params);
		} else if (flag.equals("8")) {
			// 生产班组部附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateProductDeptAttachment", params);
		} else if (flag.equals("9")) {
			// 综合管理附件
			this.getSqlMapClientTemplate().update(
					"changehistory.updateSummaryDeptAttachment", params);
		}
		return "修改附件成功";
	}

	/**
	 * 获取变更历史数量数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getExternalChangeHistoryCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"changehistory.getExternalChangeCount", params);
	}

	@Override
	public JSONObject getExternalChangeHistoryPageList(
			Map<String, String> params) {
		// TODO Auto-generated method stub

		params.put("status", "4");
		List<ExternalChangeDO> list = this.getSqlMapClientTemplate()
				.queryForList("changehistory.getExternalChanges", params);

		for (ExternalChangeDO externalChangeDO : list) {

			externalChangeDO
					.setEvaImpactForReliabilityString(IMPACT_FOR_RELIABILITY
							.get(String.valueOf(externalChangeDO
									.getEvaImpactForReliability())));
			externalChangeDO
					.setEvaImpactForProductHandleString(IMPACT_FOR_PRODUCE_HANDLE
							.get(String.valueOf(externalChangeDO
									.getEvaImpactForProductHandle())));
			externalChangeDO.setMustVerifyString(OK_MAP.get(externalChangeDO
					.getMustVerify()));
			externalChangeDO.setMustAlertCustomerString(OK_MAP
					.get(externalChangeDO.getMustAlertCustomer()));
			externalChangeDO.setMustSpplierChangeString(OK_MAP
					.get(externalChangeDO.getMustSpplierChange()));
			externalChangeDO.setResultString(EVALUATION_RESULT
					.get(externalChangeDO.getResult()));
			externalChangeDO.setSampleIsQualifiedString(OK_MAP
					.get(externalChangeDO.getSampleIsQualified()));
			externalChangeDO.setSmallIsQualifiedString(OK_MAP
					.get(externalChangeDO.getSmallIsQualified()));
			externalChangeDO.setMiddleIsQualifiedString(OK_MAP
					.get(externalChangeDO.getMiddleIsQualified()));
			externalChangeDO.setIsFinishedAlertCustomerString(OK_MAP
					.get(externalChangeDO.getIsFinishedAlertCustomer()));
			externalChangeDO.setIsFinishedSupChangeString(OK_MAP
					.get(externalChangeDO.getIsFinishedSupChange()));
			externalChangeDO.setIsFinishedSecurityString(OK_MAP
					.get(externalChangeDO.getIsFinishedSecurity()));
			externalChangeDO.setChangeResultIsQualifyString(OK_MAP
					.get(externalChangeDO.getChangeResultIsQualify()));
			externalChangeDO.setIsApproveChangeString(OK_MAP
					.get(externalChangeDO.getIsApproveChange()));

			externalChangeDO.setResearchDeptIsBomChangedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsBomChanged()));
			externalChangeDO.setResearchDeptIsDesignDocChangedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsDesignDocChanged()));
			externalChangeDO.setResearchDeptIsDrawingChnagedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsDrawingChnaged()));

			externalChangeDO.setEngineeringDeptIsBomChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsBomChanged()));
			externalChangeDO.setEngineeringDeptIsMoldChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsMoldChanged()));
			externalChangeDO.setEngineeringDeptIsProcChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsProcChanged()));
			externalChangeDO.setEngineeringDeptIsQcChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsQcChanged()));
			externalChangeDO.setEngineeringDeptIsSopChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsSopChanged()));

			externalChangeDO.setQualityDeptIsInspectDocChangedString(OK_MAP
					.get(externalChangeDO.getQualityDeptIsInspectDocChanged()));
			externalChangeDO.setQualityDeptIsMarkInFirst3String(OK_MAP
					.get(externalChangeDO.getQualityDeptIsMarkInFirst3()));
			externalChangeDO.setQualityDpetIsTrainedString(OK_MAP
					.get(externalChangeDO.getQualityDpetIsTrained()));
			externalChangeDO.setQualityDpetIsUpgradeInspectionString(OK_MAP
					.get(externalChangeDO.getQualityDpetIsUpgradeInspection()));

			externalChangeDO.setProductDeptIsMaterialCheckedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsMaterialChecked()));
			externalChangeDO.setProductDeptIsRegistedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsRegisted()));
			externalChangeDO.setProductDeptIsTrainedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsTrained()));

			externalChangeDO.setSummaryIsImpactShipMentString(OK_MAP
					.get(externalChangeDO.getSummaryIsImpactShipMent()));
			externalChangeDO
					.setSummaryIsNumberBeforeChangeProdString(OK_MAP
							.get(externalChangeDO
									.getSummaryIsNumberBeforeChangeProd()));
			externalChangeDO.setSummaryIsNumberBeforeChangeSupString(OK_MAP
					.get(externalChangeDO.getSummaryIsNumberBeforeChangeSup()));
			externalChangeDO.setStatusString(EXTERNAL_CHANGE_TICKET_STATUS
					.get(String.valueOf(externalChangeDO.getStatus())));
		}

		return JSONUtils.getPageJSON(list, this
				.getExternalChangeHistoryCount(params));
	}

}
